package cn.yy.cloudpay.util;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/27 0027.
 */
public class YimaUtil {
    private Bodyreport body = new Bodyreport();

    public void setBody(Bodyreport body) {
        this.body = body;
    }

    public String getXmlHeader(){
        String header = "<?xml version=\"1.0\" encoding=\"GBK\"?>";
        header += "<business_trans>";
        header += "</business_trans>";
        return "";
    }

    /**
     *  绑定交易
     * @param password
     * @return
     */
    public String bindipos(String password){
        StringBuffer data = new StringBuffer("");
        data.append("<store_info>");
            data.append("<store_id>"+body.getData().get("store_id")+"</store_id>");
            data.append("<store_name>"+body.getData().get("store_name")+"</store_name>");
            data.append("<store_addr>"+body.getData().get("store_addr")+"</store_addr>");
            data.append("<contact>"+body.getData().get("contact")+"</contact>");
            data.append("<contact_tel>"+body.getData().get("contact_tel")+"</contact_tel>");
            data.append("<province>"+body.getData().get("province")+"</province>");
            data.append("<city>"+body.getData().get("city")+"</city>");
            data.append("<town>"+body.getData().get("town")+"</town>");
        data.append("</store_info>");
        String datas = addCardinfo(data.toString());
        String body = addHeader(datas.toString());
        String bodyhtml = addBody(body,password);
        return bodyhtml;
    }

    /**
     *  卡券查询
     * @param password
     * @return
     */
    public String searchcardipos(String password){
        StringBuffer data = new StringBuffer("");
        data.append("<card_query_request>");
            data.append("<valid_info>"+body.getData().get("valid_info")+"</valid_info>");
            data.append("<encode_type>"+body.getData().get("encode_type")+"</encode_type>");
        data.append("</card_query_request>");
        String datas = addCardinfo(data.toString());
        String body = addHeader(datas.toString());
        String bodyhtml = addBody(body,password);
        return bodyhtml;
    }

    /**
     *  4.3	卡券验证接口
     * @param password
     * @return
     */
    public String verifycardipos(String password){
        StringBuffer data = new StringBuffer("");
        data.append("<card_verify_request>");
            data.append("<valid_info>"+body.getData().get("valid_info")+"</valid_info>");
            data.append("<encode_type>"+body.getData().get("encode_type")+"</encode_type>");
            data.append("<tx_amt>"+body.getData().get("tx_amt")+"</tx_amt>");
        data.append("</card_verify_request>");
        String datas = addCardinfo(data.toString());
        String body = addHeader(datas.toString());
        String bodyhtml = addBody(body,password);
        return bodyhtml;
    }

    /**
     *  4.4	卡券撤销接口
     * @param password
     * @return
     */
    public String cancelcardipos(String password){
        StringBuffer data = new StringBuffer("");
        data.append("<card_cancel_request>");
            data.append("<valid_info>"+body.getData().get("valid_info")+"</valid_info>");
            data.append("<encode_type>"+body.getData().get("encode_type")+"</encode_type>");
            data.append("<org_pos_seq>"+body.getData().get("org_pos_seq")+"</org_pos_seq>");
        data.append("</card_cancel_request>");
        String datas = addCardinfo(data.toString());
        String body = addHeader(datas.toString());
        String bodyhtml = addBody(body,password);
        return bodyhtml;
    }

    /**
     * 4.5	卡券冲正接口
     * @param password
     * @return
     */
    public String reversalcardipos(String password){
        StringBuffer data = new StringBuffer("");
        data.append("<card_reversal_request>");
            data.append("<valid_info>"+body.getData().get("valid_info")+"</valid_info>");
            data.append("<encode_type>"+body.getData().get("encode_type")+"</encode_type>");
            data.append("<org_pos_seq>"+body.getData().get("org_pos_seq")+"</org_pos_seq>");
        data.append("</card_reversal_request>");
        String datas = addCardinfo(data.toString());
        String body = addHeader(datas.toString());
        String bodyhtml = addBody(body,password);
        return bodyhtml;
    }

    /**
     *4.6	卡券日结交易
     * @param password
     * @return
     */
    public String dailycardipos(String password){
        StringBuffer data = new StringBuffer("");
        data.append("<card_daily_request>");
        data.append("<daily_day>"+body.getData().get("daily_day")+"</daily_day>");
        data.append("</card_daily_request>");
        String datas = addCardinfo(data.toString());
        String body = addHeader(datas.toString());
        String bodyhtml = addBody(body,password);
        return bodyhtml;
    }

    private String addCardinfo(String datas){
        StringBuffer header = new StringBuffer("");
        header.append("<request_type>"+body.getRequest_type()+"</request_type>");
        header.append("<isspid>"+body.getIsspid()+"</isspid>");
        header.append("<pos_id>"+body.getRequest_type()+"</pos_id>");
        header.append("<store_id>"+body.getStore_id()+"</store_id>");
        header.append("<pos_seq>"+body.getPos_seq()+"</pos_seq>");
        header.append("<user_id>"+body.getUser_id()+"</user_id>");
        header.append("<system_id>"+body.getSystem_id()+"</system_id>");
        header.append(datas);
        return header.toString();
    }

    private String addHeader(String body){
        StringBuffer header = new StringBuffer("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        header.append("<business_trans>");
        header.append(body);
        header.append("</business_trans>");
        return header.toString();
    }


    private String addBody(String body,String passwrd){
        try {
            MD5 md5 = new MD5();
            String xmlhtml = "xml="+URLEncoder.encode(body,"GBK");
            String machtml = "mac="+md5.getMD5ofStr(passwrd+body+passwrd);
            return xmlhtml + "&" + machtml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    class Bodyreport{
        private String request_type;
        private String isspid;
        private String pos_id;
        private String store_id;
        private String pos_seq;
        private String user_id;
        private String system_id;
        private Map<String,String> data = new HashMap<String,String>();//终端绑定交易

        public String getRequest_type() {
            return request_type;
        }

        public void setRequest_type(String request_type) {
            this.request_type = request_type;
        }

        public String getPos_seq() {
            return pos_seq;
        }

        public void setPos_seq(String pos_seq) {
            this.pos_seq = pos_seq;
        }

        public String getUser_id() {
            return user_id;
        }

        public void setUser_id(String user_id) {
            this.user_id = user_id;
        }

        public String getIsspid() {
            return isspid;
        }

        public void setIsspid(String isspid) {
            this.isspid = isspid;
        }

        public String getPos_id() {
            return pos_id;
        }

        public void setPos_id(String pos_id) {
            this.pos_id = pos_id;
        }

        public String getStore_id() {
            return store_id;
        }

        public void setStore_id(String store_id) {
            this.store_id = store_id;
        }

        public String getSystem_id() {
            return system_id;
        }

        public void setSystem_id(String system_id) {
            this.system_id = system_id;
        }

        public Map<String, String> getData() {
            return data;
        }

        public void setData(Map<String, String> data) {
            this.data = data;
        }
    }
}
